﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quellenverwaltung.Word;
using System.Globalization;

namespace Quellenverwaltung.Meta
{
    public class BuildWebsiteSource
    {
        Configuration configuration;

        public BuildWebsiteSource()
        {
            configuration = Configuration.getConfiguration();
        }

        public Data.Source.Website FromMetaData(Dictionary<string, string> meta)
        {
            Data.Source.Website website = new Data.Source.Website();

            // ToDo <title>

            // Process Article Date
            DateTime? date = getDate(meta, configuration.Tags_Date);
            if (date != null) {
                website.Day = ((DateTime)date).Day.ToString();
                website.Month = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(((DateTime)date).Month);
                website.Year = ((DateTime)date).Year.ToString();
            }

            // Process Accessed Date
            DateTime currentDate = DateTime.Now;
            if (currentDate != null)
            {
                website.DayAccessed = currentDate.Day.ToString();
                website.MonthAccessed = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(currentDate.Month);
                website.YearAccessed = currentDate.Year.ToString();
            }

            website.InternetSiteTitle = getString(meta, configuration.Tags_SiteName);
            string websiteTitle = getString(meta, configuration.Tags_Title);
            if (String.IsNullOrEmpty(website.InternetSiteTitle))
            {
                foreach (string del in configuration.Tags_SplitTitleToSiteName)
                {
                    if (websiteTitle.Contains(del))
                    {
                        int index = websiteTitle.LastIndexOf(del);
                        website.Title = websiteTitle.Substring(0, index).Trim();
                        website.InternetSiteTitle = websiteTitle.Substring(index + del.Count()).Trim();
                        break;
                    }
                }
            }
            if (String.IsNullOrEmpty(website.Title))
            {
                website.Title = websiteTitle;
            }
            website.Url = getString(meta, configuration.Tags_Url);
            website.ProductionCompany = getString(meta, configuration.Tags_Publisher);

            string authornames = getString(meta, configuration.Tags_Author);
            if (authornames != null && authornames.Length > 0)
            {
                website.AuthorNamesAsString = authornames;
                if (website.AuthorNamesAsString.Count() < authornames.Length * 0.8)
                {
                    // Conversion propably failed. -> use Corporate instead
                    website.AuthorCorporateAsString = authornames;
                }
            }

            return website;
        }

        private DateTime? getDate(Dictionary<string, string> meta, String[] tags)
        {
            DateTime? dateTime = null;
            string dateVal;

            foreach (String key in tags) 
            {
                if (meta.TryGetValue(key, out dateVal))
                {
                    if (dateVal.Length > 0)
                    {
                        try
                        {
                            dateTime = DateTime.Parse(dateVal);
                            if (dateTime != null)
                            {
                                break;
                            }
                        }
                        catch { };
                    }
                }
            }

            return dateTime;
        }

        public string getString(Dictionary<string, string> meta, String[] tags)
        {
            string val;

            foreach (String key in tags)
            {
                if (meta.TryGetValue(key, out val))
                {
                    if (val != null && val.Length > 0)
                    {
                        return val;
                    }
                }
            }

            return "";
        }
    }
}
